<?php

namespace App\Http\Controllers\Api\Members;

use App\Http\Controllers\Controller;
use App\Libs\Strategy\Context;
use App\Models\Members\MemberPipeline;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class MemberPipelineController extends Controller
{
    private $member_pipeline;

    public function __construct()
    {
        $this->member_pipeline = new Context(new MemberPipeline());
    }

    // 我的流水
    public function show($id)
    {
        $flow = $this->member_pipeline->display(request()->limit,function($query) use ($id){
            $query->with('member');
            $query->where('member_id',$id);
            if(request()->type)
            {
                $query->where('type',request()->type);
            }
            if(isset(request()->time[0]) && !empty(request()->time[0])){
                $query->whereBetween('created_at', request()->time);
            }
            return $query;
        });
        $count = $this->member_pipeline->getModel()->where(function($query){
            if(request()->type)
            {
                $query->where('type',request()->type);
            }
            if(isset(request()->time[0]) && !empty(request()->time[0])){
                $query->whereBetween('created_at', request()->time);
            }
        })->where('member_id',$id)->whereIn('type',[1,2,3,4,11])->sum('money');;
        return $this->success([
            'money' => Str::after($count,'-'),
            'flow' => $flow
        ]);
    }

    //我的团队流水
    public function store(){
        $flow = $this->member_pipeline->display(request()->limit,function($query) use ($id){
            $query->where('member_id',$id);
        });
        $flow->each(function($item){
            $item->type = $this->pipelineType[$item->type] ?? '未知';
            return $item;
        });
        return $this->success($flow);
    }

}
